﻿namespace Bildverarbeitung
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;

    using Bildverarbeitung.CGFilter;

    /// <summary>
    /// Comparizon of ImageExtract filters - get the best out of many images.
    /// </summary>
    /// <para>Sample usage:</para>
    /// <code>
    /// </code>
    /// 
    public class CGBildverarbeitung
    {
        /// <summary>
        /// Gets or sets the array of images.
        /// </summary>
        /// <value>The images.</value>
        public Bitmap[] Images { get; set; }

        /// <summary>
        /// path to images
        /// </summary>
        public String imagepath = "";

        /// <summary>
        /// Initializes images given in specified path.
        /// </summary>
        /// <param name="path">Path to images.</param>
        /// <returns>Array of images. </returns>
        public Bitmap[] InitializeImages(String path)
        {
            if (path == null)
            {
                return null;
            }

            if (path.Equals(""))
            {
                return null;
            }

            if (this.imagepath.Equals(""))
            {
                this.imagepath = path;
            }

            DirectoryInfo dirInfo = new DirectoryInfo(@path);
            if (dirInfo == null)
            {
                return null;
            }

            FileInfo[] files = dirInfo.GetFiles();

            if (files == null)
            {
                return null;
            }

            this.Images = new Bitmap[files.Length];
            int i = 0;

            foreach (FileInfo fiOutput in files)
            {
                try
                {
                    System.Drawing.Bitmap image = (Bitmap)Bitmap.FromFile(path + "\\" + fiOutput.Name);
                    AForge.Imaging.Image.FormatImage(ref image);
                    Images[i++] = image;
                }
                catch (System.OutOfMemoryException e)
                {
                    System.Console.Out.WriteLine("OutOfMemoryException " + e.Message);
                    return null;
                }
                catch (System.IO.FileNotFoundException e)
                {
                    System.Console.Out.WriteLine("FileNotFoundException" + e.Message);
                    return null;
                }
                catch (System.ArgumentException e)
                {
                    System.Console.Out.WriteLine("ArgumentException" + e.Message);
                    return null;
                }
            }
            return Images;
        }

        /// <summary>
        /// List of all SKGDV Filter
        /// </summary>
        public List<Bildverarbeitung.IFilter> CGFilter = new List<Bildverarbeitung.IFilter>();
        /// <summary>
        /// Initializes the SKGEV filters.
        /// </summary>
        /// <returns></returns>
        public int InitializeSKGEVFilters()
        {
           // CGFilter.Add(new GrimmPaulFilter());
           // CGFilter.Add(new HesseSebastianFilter());
            //aouainia
            CGFilter.Add(new FessiAymenFilter());//aymen
            //Bott
            //Depta
            CGFilter.Add(new CG_Door_Feature_Extraction.CGFilter.EnglertMichaelFilter());
            CGFilter.Add(new HaasBastianDoorFilter()); //Haas
            CGFilter.Add(new HeinzPatrikFilter());
            CGFilter.Add(new HesseSebastianFilter());
            ////kamdem tonkale
            CGFilter.Add(new KellerChristopher());
            ////KIEU
          //  CGFilter.Add(new KlomannMarcelFilter());
            ////Kömpel
            ////Kömpel
            ////CGFilter.Add(new KömpelMichaelFilter());
            ////kouada
            CGFilter.Add(new KraftMatthiasFilter());
            //CGFilter.Add(new KuebrichAndiFilter());
            ////Kunz
            ////Li
            ////Lommel
            ////Mani
            ////Nangue Kapnang
            ////Ngo
            ////nguentcheng motcheyo
            ////Paulheim
            CGFilter.Add(new PaulheimAnnikaFilter());
            CGFilter.Add(new RichardtManuelFilter());
            ////Ruppel
            CGFilter.Add(new SlimeneTaycirFilter());////Slimene
            ////Tobert
            CGFilter.Add(new SasenoWidyaFilter());//widya

            System.Console.Out.WriteLine(CGFilter.Count + " filter added \n");
            return CGFilter.Count;
        }

        /// <summary>
        /// Uses the SKGDV filters.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        public Bitmap UseSingleSKGDVFilters(IFilter filter)
        {
            Bitmap image = null;

            try
            {
                image = filter.GetImageWithDoor(Images[7]);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Exception " + e.Message);
                return null;
            }
            return image;
        }

        /// <summary>
        /// Uses the SKGDV filters.
        /// </summary>
        /// <returns></returns>
        public int UseSKGDVFilters()
        {
            long startTime, endTime, time;
            int successes = 0;

            foreach (IFilter filter in CGFilter)
            {
                InitializeImages(imagepath);
                startTime = DateTime.Now.Ticks;
                Bitmap image = UseSingleSKGDVFilters(filter);
                endTime = DateTime.Now.Ticks;
                time = endTime - startTime;
                time /= 10000;
                System.Console.Out.WriteLine(time.ToString() + " " + filter.GetName() + " " + successes);

                try
                {
                    if (image != null)
                    {
                        image.Save(filter.GetName() + time.ToString() + ".png");
                        successes++;
                    }
                    else
                    {
                        System.Console.WriteLine("Error: No image saved: " + filter.GetName());
                    }
                }
                catch (ArgumentNullException e)
                {
                    System.Console.WriteLine(filter.GetName() + ": ArgumentNullException " + e.Message);
                }
                catch (System.Runtime.InteropServices.ExternalException e)
                {
                    System.Console.WriteLine(filter.GetName() + ": ExternalException " + e.Message);
                }
            }
            return successes;
        }
    }
}